package ar.edu.itba.it.paw.domain;

import java.util.HashSet;
import java.util.Set;


import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.OneToMany;


@Entity
public class Category extends PersistentAttributes {

	private String name;
	@OneToMany(cascade=CascadeType.ALL, mappedBy = "category")
	private Set<Product> product_set;
	

	protected Category(){

	}
	
	public Category(int product_id, String name){
		if(!(product_id < 0 ) && name != null && !name.equals(""))
		{
			this.setId(product_id);
			this.setName(name);
			product_set = new HashSet<Product>();
		}
		else
		{
			throw new IllegalArgumentException();
		}
	}

	public String getName() {
		return name;
	}

	private void setName(String name) {
		if(name != null && !name.equals(""))
			this.name = name;
		else
			throw new IllegalArgumentException();
	}

	public Set<Product> getProduct_set() {
		return product_set;
	}

//	private void setProduct_set(Set<Product> product_set) {
//		if(product_set == null)
//			throw new IllegalArgumentException();
//		else
//			this.product_set = product_set;
//	}
	
	@Override
	public String toString() {
		return "Category [name=" + name + "]";
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Category other = (Category) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
}
